<?php
class App extends Module {
    private $site = false;
    private $base_url = false;

    public function run() {
        $this->_load_site_setting();
        
        $router = $this->get_router();
        $m = $router['m'];
        $a = 'act_' . $router['a'];
        
        $module = m($m);
        if ( ! method_exists($module, $a)) {
            tos_die('error request' . $m . ' => ' . $a);
        }
        $module->$a();
        $site = $this->get_site();
        $out = out();
        
        $out->add_title($site['caption']);
        $out->display(tos_http_get('method'));
        
    }
    
    public function get_router() {
        $m = tos_http_get('m');
        $a = tos_http_get('a');
        if (empty($m) || empty($a)) {
            $site = $this->get_site();
            $m = $site['router']['m'];
            $a = $site['router']['a'];
        }
        
        $action_access = m($m)->get_action_access($a);
        if ($action_access != ACTION_ACCESS_PUBLIC) {
            if ( ! m('user')->logined()) {
                $m = 'user';
                $a = 'login';
            }
        }
        return array('m' => $m, 'a' => $a);
    }
    
    
    public function get_site() {
        return $this->site;
    }
    
    
    public function get_site_caption() {
        return $this->site['caption'];
    }
    
    
    public function get_base_url() {
        if ( ! $this->base_url) {
            $site = $this->get_site();
            $base_url = trim($site['server_name'], '/');
            $base_path = trim($site['base_path'], '/');
            if ($base_path) 
                $base_url .= '/' . $base_path;
            $this->base_url = $base_url;
        }
        return $this->base_url;
    }
    
    
    private function _load_site_setting() {
        if ( ! $this->site) {
            $server_name = $_SERVER['SERVER_NAME'];
            $dir = ROOT_DIR . '/site/';
            $file = $dir . $server_name . '.setting.php';
            if ( ! file_exists($file)) {
                $file = $dir . 'default.setting.php';
            }
            $setting = include $file;
            
            $site = array(
                'caption'       => $setting['caption'],
                'level'         => $setting['level'],
                'router'        => $setting['router'],
                'server_name'   => trim($server_name),
                'base_path'     => isset($setting['base_path']) ? trim($setting['base_path']) : ''
            );
            if (isset($setting['thumbnail'])) {
                $thumbnail = trim($setting['thumbnail']);
                if ( ! empty($thumbnail)) {
                    $site['thumbnail'] = $setting['thumbnail'];
                }
            }
            
            if (isset($setting['cdn'])) {
                m('cdn')->set_opts($setting['cdn']);
            }
            
            $out = out();
            
            if (isset($setting['css'])) {
                $csses = $setting['css'];
                foreach ($csses as $css) {
                    $out->add_css(tos_cdn_url($css));
                }
            }
            if (isset($setting['js'])) {
                $jses = $setting['js'];
                foreach ($jses as $js) {
                    $out->add_prejs(tos_cdn_url($js));
                }
            }
            if (isset($setting['block'])) {
                $boxes = $setting['block'];
                foreach ($boxes as $box_name => $box) {
                    foreach ($box as $block) {
                        $out->add_block(new View($block), $box_name);
                    }
                }
            }
            if (isset($setting['node_type'])) {
                m('node')->add_types($setting['node_type']);
            }
            if (isset($setting['theme'])) {
                tos_set_config('theme', $setting['theme']);
            }
            if (isset($setting['databases'])) {
                $databases = $setting['databases'];
                foreach ($databases as $keyword => $database) {
                    tos_add_db($keyword, $database);
                }
            }
            if (isset($setting['debug_mode']) && $setting['debug_mode'] == true) {
                tos_debug_mode(true);
            }
            $this->site = $site;
        }
    }
}
?>